package com.example.finalapplication;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.SimpleAdapter;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import java.util.ArrayList;
import java.util.HashMap;

public class AnimalActivity extends AppCompatActivity {
    ProgressBar bar;
    ListView listView;
    Handler handler;
   //Adapter AnimalAdapter;
    SimpleAdapter listItemAdapter;

    ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_animal);
        Intent intent1 = getIntent();
        listView = findViewById(R.id.mylist2);

        bar = findViewById(R.id.progressBar2);
       listView.setEmptyView(bar);
        ArrayList<HashMap<String, String>> listItems = new ArrayList<HashMap<String, String>>();
        listItemAdapter = new SimpleAdapter(this,
                listItems,
                R.layout.animal_item,
                new String[]{"ItemName", "ItemEname", "ItemKe", "ItemGang"},
                new int[]{R.id.name, R.id.Ename, R.id.Ke, R.id.Gang}
        );

        listView.setAdapter(listItemAdapter);
     //   listView.setOnItemClickListener(this);
      //  listView.setOnItemLongClickListener(this);
          handler = new Handler(Looper.myLooper()) {

            @Override
            public void handleMessage(@NonNull Message msg) {
                if (msg.what == 6) {
                    list = (ArrayList<HashMap<String, String>>) msg.obj;
                    listItemAdapter = new SimpleAdapter(AnimalActivity.this,
                            list,
                            R.layout.animal_item,
                            new String[]{"name", "Ename","Ke","Gang"},
                            new int[]{R.id.name, R.id.Ename, R.id.Ke, R.id.Gang});
                      AnimalAdapter AnimalAdapter = new AnimalAdapter(AnimalActivity.this,
                            R.layout.animal_item,
                             list);

                   listView.setAdapter(listItemAdapter);
        //AnimalAdapter adapter = new AnimalAdapter(itemData);
       // LinearLayoutManager layoutManager = new LinearLayoutManager(AnimalActivity.this);
      //  RecyclerView.setLayoutManager(layoutManager);
       // RecyclerView.setAdapter(adapter);

                }
                super.handleMessage(msg);
            }
        };


       AnimalTask animalTask = new AnimalTask();
        animalTask.setHandler(handler);

        Thread t = new Thread(animalTask);
        t.start();


       // RecyclerView.setAdapter(AnimalAdapter);



   //     RecyclerView  = findViewById(R.id.mylist2);
     //   AnimalAdapter adapter = new AnimalAdapter(itemData);

         //  AnimalAdapter.setLayoutManager(new LinearLayoutManager(this));
          //  RecyclerView.setAdapter(adapter);
    }

   /* public void run() {
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        ArrayList<HashMap<String,String>> retList = new ArrayList<HashMap<String,String>>();
        try {
            // Document doc = Jsoup.connect("https://www.usd-cny.com/bankofchina.htm").get();
            Document doc = (Document) Jsoup.connect("https://www.maigoo.com/news/522950.html").get();

            String title = doc.title();
            Log.i(TAG, "run:title=" + title);

            Elements trs = doc.getElementsByTag("tr");
            for (Element tr : trs) {
                Elements tds = tr.getElementsByTag("td");
                Log.i(TAG, "run:td.size=" + tds.size());
                if (tds.size() > 0) {
                    String td1 = tds.get(1).text();
                    String td2 = tds.get(2).text();
                    String td3 = tds.get(3).text();
                    String td4 = tds.get(4).text();
                    Log.i(TAG, "run:" + td1 + "==>" + td4);


                    //构造map放入数据
                    HashMap<String,String> map = new HashMap<String,String>();
                    map.put("name",td1);
                    map.put("Ename",td2);
                    map.put("Ke",td3);
                    map.put("Gang",td4);

                    retList.add(map);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }



        Message msg = handler.obtainMessage(6, retList);
        //msg.obj = "Hello";

        handler.sendMessage(msg);
        Log.i(TAG, "run:sendMessage ok");
    }*/
}
